#include "tictactoe.h"




tictactoe::tictactoe(void)
{
	for(int i = 0; i < 3; i++){
		for(int j = 0; j < 3; j++){
			board[i][j] = ' ';
		}
	}
	xcursor = 0;
	ycursor = 0;
	turn = 0;
}


tictactoe::~tictactoe(void)
{
}

void tictactoe::drawBoard(){
	system("cls");
	cout << "-------" << endl;
	for(int i = 0; i < 3; i++){
		cout << "!";
		for(int j = 0; j < 3; j++){
			cout << board[i][j] << "!";
		}
		cout << endl << "-------" << endl;
	}
}

void tictactoe::play(){
	int ch;
	int playing = 1;
	while(playing){
		Console::gotoxy(xcursor * 2 + 1,ycursor * 2 + 1);
		ch = _getch();
		switch(ch){
			case KEY_DOWN:
				if(ycursor < 2)
					ycursor += 1;
				break;
			case KEY_UP:
				if(ycursor > 0)
					ycursor -= 1;
				break;
			case KEY_LEFT:
				if(xcursor > 0)
					xcursor -= 1;
				break;
			case KEY_RIGHT:
				if(xcursor < 2)
					xcursor += 1;
				break;
			case '\r':
				if(turn == 0){
					board[ycursor][xcursor] = 'x';
					turn = 1;
				}
				else{
					board[ycursor][xcursor] = 'o';
					turn = 0;
				}
				playing = 0;
				break;
			default:
				break;
		}
		
		
	}
}

int tictactoe::checkWin(){
	for(int i = 0; i < 3; i++){
		if(board[i][0] != ' ' && board[i][0] == board[i][1] && board[i][0] == board[i][2]){
			return board[i][0];
		}
		else if(board[0][i] != ' ' && board[0][i] == board [1][i] && board[0][i] == board[2][i]){
			return board[0][i];
		}
	}
	if(board[0][0] != ' ' && board[0][0] == board[1][1] && board[0][0] == board[2][2]){
		return board[0][0];
	}
	else if(board[2][0] != ' ' && board[2][0] == board[1][1] && board[2][0] == board[0][2]){
		return board[2][0];
	}
	for(int i = 0; i < 3; i++){
		for(int j = 0; j < 3; j++){
			if(board[i][j] == ' '){
				return 0;
			}
		}
	}
	return 'd';
}